package com.example.kekewaimaibao.goods.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kekewaimaibao.core.constant.RestConstant;
import com.example.kekewaimaibao.goods.model.GoodsDO;
import com.example.kekewaimaibao.goods.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: xuxiang
 * @date: 2022/3/6 22:55 星期日
 * @description: 商品控制层
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping(GoodsController.PATH)
@Api(tags = "商品控制层")
@CrossOrigin
public class GoodsController {
    public static final String PATH = RestConstant.VERSION_V1 + "/goods";

    @Autowired
    IGoodsService goodsService;

    /**
     * 建议弃用全表查询
     * @return
     */
    @ApiOperation(value = "所有商品列表")
    @GetMapping("/list")
    public R list() {
        List<GoodsDO> list = goodsService.list(null);
        return R.ok(list);
    }

    /**
     * 根据商品名称模糊分页
     * @param current
     * @param limit
     * @param goodsDO
     * @return
     */
    @ApiOperation(value = "分页查询商品信息")
    @PostMapping("/page/{current}/{limit}")
    public R page(@PathVariable long current, @PathVariable long limit,
                  @RequestBody(required = false) GoodsDO goodsDO) {
        Page<GoodsDO> page = new Page<>(current, limit);
        QueryWrapper<GoodsDO> wrapper = null;
        if (ObjectUtil.isNotNull(goodsDO)) {
            wrapper = new QueryWrapper<>();
            if (StrUtil.isNotBlank(goodsDO.getName())) {
                wrapper.like("name", goodsDO.getName());
            }
        }
        goodsService.page(page, wrapper);
        long total = page.getTotal();
        List<GoodsDO> records = page.getRecords();
        Map map = new HashMap<>(4);
        map.put("current", current);
        map.put("limit", limit);
        map.put("total", total);
        map.put("records", records);
        return R.ok(map);
    }

    @ApiOperation(value = "添加商品信息")
    @PostMapping("/save")
    public R save(@RequestBody GoodsDO goodsDO) {
        boolean flag = goodsService.save(goodsDO);
        if (flag) {
            return R.ok("后台添加商品信息成功");
        } else {
            return R.failed("后台添加商品信息失败");
        }
    }

    @ApiOperation(value = "删除商品信息")
    @PostMapping("/remove/{id}")
    public R remove(@PathVariable Long id) {
        boolean flag = goodsService.removeById(id);
        if (flag) {
            return R.ok("后台删除id为" + id +"的商品信息成功");
        } else {
            return R.failed("后台删除商品信息失败");
        }
    }

    @ApiOperation(value = "更新商品信息")
    @PostMapping("/update")
    public R update(@RequestBody GoodsDO goodsDO) {
        boolean flag = goodsService.updateById(goodsDO);
        if (flag) {
            return R.ok("后台更新id为" + goodsDO.getGoodsId() + "的商品信息成功");
        } else {
            return R.failed("后台更新商品信息失败");
        }
    }

    @ApiOperation(value = "批量删除商品信息")
    @PostMapping("/remove-ids/{ids}")
    public R deleteBatchIds(@PathVariable("ids")String[] ids) {
        List<String> list = Arrays.asList(ids);
        boolean flag = goodsService.removeByIds(list);
        if (flag) {
            return R.ok("批量删除商品信息成功");
        } else {
            return R.failed("批量删除商品信息失败");
        }
    }

    @ApiOperation(value = "通过id查找商品信息")
    @GetMapping("/get-by-id/{id}")
    public R getByShopId(@PathVariable Long id) {
        GoodsDO goodsDO = goodsService.getById(id);
        if (ObjectUtil.isNotNull(goodsDO)) {
            return R.ok(goodsDO);
        } else {
            return R.failed("未找到id为" + id + "的商品");
        }
    }

    /**
     * 通过shopId查询商品列表
     * @param id
     * @return
     */
    @ApiOperation(value = "通过商家ID获取商品列表")
    @GetMapping("/get-by-shop/{id}")
    public R getByShop(@PathVariable Long id) {
        QueryWrapper<GoodsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", id);
        List<GoodsDO> list = goodsService.list(wrapper);
        return R.ok(list);
    }
}
